<%@ Page Language="C#" MasterPageFile="~/master/admin.master" AutoEventWireup="true"
    CodeFile="sales-ccfail.aspx.cs" Inherits="admin_sales_ccfail" Title="Sales Failed" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<%@ Register Namespace="ECommerce.Library.Web" TagPrefix="ucc" %>
<%@ Register Src="~/controls/wucAdminValidator.ascx" TagName="AdminValidator" TagPrefix="wuc" %>
<asp:Content ID="cntMiddleContent" ContentPlaceHolderID="cphMiddleContent" runat="Server">
    <script type="text/javascript">

        var OrderCode_Renderer = function (value, p, record, rowIndex) {
            var categoryUrl;
            if (record.data.IsMultipleShip) {
                categoryUrl = String.format("shipping-list.aspx?oid={0}", record.data.OrderId);
                categoryUrl = String.format("<a href=\"{0}\" class=\"leftlink\">{1}</a>", categoryUrl, value);
            }
            else {
                categoryUrl = String.format("<%= Page.ResolveUrl(AutoGear.BLL.CustomSiteMap.InvoicePrintPage) %>?print=0&oid={0}", record.data.OrderCode);
                categoryUrl = String.format("<a href=\"{0}\" class=\"leftlink\" target=\"_blank\">{1}</a>", categoryUrl, "13-P" + value);
            }

            return categoryUrl;
        };
        var SPName_Renderer = function (value, p, record, rowIndex) {
            var categoryUrl;
            categoryUrl = String.format("sales-person-edit.aspx?cuid={0}", record.data.SPId);
            categoryUrl = String.format("<a href=\"{0}\" class=\"leftlink\" target=\"_blank\">{1}</a>", categoryUrl, record.data.SPName);
            return categoryUrl;
        };
        var Date_Renderer = function (value) {
            var dt = new Date(value);
            return dt.format('m/d/Y');
        }

        var CustomerName_Renderer = function (value, p, record, rowIndex) {
            return record.data.FirstName + " " + record.data.LastName;
        }
        var Address_Renderer = function (value, p, record, rowIndex) {
            return record.data.BillingAddressLine1 + ", " + record.data.City + ", " + record.data.State + ", " + record.data.Zip;
        }
        var CommandHandler = function (command, rowIndex, grid) {
            //showObj(grid);
            grid.getRowEditor().stopEditing();
            grid.getView().refresh();
            grid.getSelectionModel().selectRow(rowIndex);
            grid.getRowEditor().startEditing(rowIndex);
        }

        var afterEdit = function (e) {

            e.grid.stopEditing();
            var payMethod = $("#<%= txtModeOfPayment.ClientID %>").val();
            //alert(pay);
            // Call DirectMethod
            Polana.WebService.UpdateOrder(e.record.data.OrderId, payMethod);
        };

        var categoryGrid;

        var deleteOrders = function () {
            if (categoryGrid != null) {
                categoryGrid.getRowEditor().stopEditing();

                var s = categoryGrid.getSelectionModel().getSelections();
                if (s.length > 0) {

                    if (confirm("Are you sure you want to delete these orders?")) {

                        var ids = "";

                        for (var i = 0, r; r = s[i]; i++) {
                            ids = ids + " " + r.data.OrderId;
                        }

                        // Call DirectMethod
                        Polana.WebService.DeleteOrders(ids);
                    }
                }
            }
        }

        var searchOrder = function (grid) {
            Polana.WebService.SearchOrder();
        }

        var onDocumentReady = function (grid) {
            categoryGrid = grid;
        }

    </script>
    <ext:ResourceManager ID="ScriptManager1" runat="server" Theme="Gray">
        <Listeners>
            <DocumentReady Fn="onDocumentReady(#{GridPanel1})" />
        </Listeners>
    </ext:ResourceManager>
    <ext:Store ID="Store1" runat="server" RemoteSort="true" OnRefreshData="Store1_RefreshData">
        <AutoLoadParams>
            <ext:Parameter Name="sort" Value="OrderId desc" Mode="Value" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="limit" Value="100" Mode="Raw" />
        </AutoLoadParams>
        <Proxy>
            <ext:PageProxy />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="OrderId">
                <Fields>
                    <ext:RecordField Name="OrderId" />
                    <ext:RecordField Name="OrderCode" ServerMapping="OrderNumber" />
                    <ext:RecordField Name="CreateDate" ServerMapping="OrderDate" Type="Date" />
                    <ext:RecordField Name="FirstName" ServerMapping="BillingAddress.FirstName" />
                    <ext:RecordField Name="LastName" ServerMapping="BillingAddress.LastName" />
                    <ext:RecordField Name="EmailAddress" ServerMapping="BillingAddress.EmailAddress" />
                    <ext:RecordField Name="BillingAddressLine1" ServerMapping="BillingAddress.AddressLine1" />
                    <ext:RecordField Name="City" ServerMapping="BillingAddress.City" />
                    <ext:RecordField Name="State" ServerMapping="BillingAddress.AddressState" />
                    <ext:RecordField Name="Zip" ServerMapping="BillingAddress.ZipCode" />
                    <ext:RecordField Name="OrderPrice" ServerMapping="OrderAmount" />
                    <ext:RecordField Name="TotalAmount" ServerMapping="TotalAmount"  />
                    <ext:RecordField Name="PaymentType" ServerMapping="PaymentType" />
                    <ext:RecordField Name="IsMultipleShip" ServerMapping="IsMultipleShip" />
                    <ext:RecordField Name="SpecialInstruction" />
                    <ext:RecordField Name="SPName" ServerMapping="SPName">
                    </ext:RecordField>
                    <ext:RecordField Name="SPId">
                    </ext:RecordField>
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <div class="admin-function-top inline">
        <ucc:ResponseMessage ID="uccResponseMessage" runat="server"></ucc:ResponseMessage>
        <ext:GridPanel ID="GridPanel1" runat="server" EnableViewState="true" AutoHeight="true"
            StoreID="Store1">
            <ColumnModel>
                <Columns>
                    <ext:Column Header="Order #" Width="20" DataIndex="OrderCode">
                        <Renderer Fn="OrderCode_Renderer" />
                    </ext:Column>
                    <ext:Column Header="Date" Width="30" DataIndex="CreateDate">
                        <Renderer Fn="Date_Renderer" />
                    </ext:Column>
                    <ext:Column Header="Name" DataIndex="FirstName" Width="50">
                        <Renderer Fn="CustomerName_Renderer" />
                    </ext:Column>
                    <ext:Column Header="Email" DataIndex="EmailAddress" Width="50">
                    </ext:Column>
                    <ext:Column Header="Address" DataIndex="BillingAddressLine1" Align="Left" Width="100">
                        <Renderer Fn="Address_Renderer" />
                    </ext:Column>
                    <ext:Column Header="Total Price" DataIndex="TotalAmount" Width="27" Align="Right">
                        <Renderer Format="UsMoney" />
                    </ext:Column>
                    <ext:Column Header="Payment Method" DataIndex="PaymentType" Width="40">
                    </ext:Column>
                    <ext:CommandColumn Width="70" DataIndex="SpecialInstruction">
                        <Commands>
                            <ext:GridCommand Icon="ApplicationEdit" CommandName="ManualAuth" Text="Manual Authorization" />
                        </Commands>
                        <Editor>
                            <ext:MultiField runat="server">
                                <Fields>
                                    <ext:Label ID="Label4" Text="Mode of payment accepted" runat="server" />
                                    <ext:TextField ID="txtModeOfPayment" runat="server" DataIndex="SpecialInstruction" />
                                </Fields>
                            </ext:MultiField>
                        </Editor>
                    </ext:CommandColumn>
                
                  <%--  <ext:Column Header="Print" Fixed="true" Sortable="false" ColumnID="label" MenuDisabled="true"
                        Width="80" DataIndex="OrderCode" Align="Right">
                        <Renderer Fn="Invoice_Renderer" />
                    </ext:Column>--%>
                </Columns>
            </ColumnModel>
            <TopBar>
                <ext:Toolbar ID="SearchToolbar2" runat="server" AutoScroll="true">
                    <Items>
                        <ext:DateField ID="txtDateFrom" runat="server" EmptyText="Date From" EnableKeyEvents="true">
                            <Listeners>
                                <KeyPress Handler="if(e.getKey() == Ext.EventObject.ENTER){e.preventDefault(); searchOrder(#{GridPanel1})}" />
                            </Listeners>
                        </ext:DateField>
                        <ext:ToolbarSpacer ID="ToolbarSpacer1" runat="server" Width="10px" />
                        <ext:DateField ID="txtDateTo" runat="server" EmptyText="Date To" EnableKeyEvents="true">
                            <Listeners>
                                <KeyPress Handler="if(e.getKey() == Ext.EventObject.ENTER){e.preventDefault(); searchOrder(#{GridPanel1})}" />
                            </Listeners>
                        </ext:DateField>
                        <ext:ToolbarSpacer ID="ToolbarSpacer2" runat="server" Width="10px" />
                        <ext:NumberField ID="txtPriceFrom" runat="server" DecimalPrecision="2" EmptyText="Price From"
                            EnableKeyEvents="true">
                            <Listeners>
                                <KeyPress Handler="if(e.getKey() == Ext.EventObject.ENTER){e.preventDefault(); searchOrder(#{GridPanel1})}" />
                            </Listeners>
                        </ext:NumberField>
                        <ext:ToolbarSpacer ID="ToolbarSpacer3" runat="server" Width="10px" />
                        <ext:NumberField ID="txtPriceTo" runat="server" EmptyText="Price To" EnableKeyEvents="true">
                            <Listeners>
                                <KeyPress Handler="if(e.getKey() == Ext.EventObject.ENTER){e.preventDefault(); searchOrder(#{GridPanel1})}" />
                            </Listeners>
                        </ext:NumberField>
                        <ext:ToolbarSpacer ID="ToolbarSpacer4" runat="server" Width="10px" />
                        <ext:TextField ID="txtFirstName" runat="server" EmptyText="First Name" EnableKeyEvents="true">
                            <Listeners>
                                <KeyPress Handler="if(e.getKey() == Ext.EventObject.ENTER){e.preventDefault(); searchOrder(#{GridPanel1})}" />
                            </Listeners>
                        </ext:TextField>
                        <ext:ToolbarSpacer ID="ToolbarSpacer5" runat="server" Width="10px" />
                        <ext:TextField ID="txtEmail" runat="server" EmptyText="Email" EnableKeyEvents="true">
                            <Listeners>
                                <KeyPress Handler="if(e.getKey() == Ext.EventObject.ENTER){e.preventDefault(); searchOrder(#{GridPanel1})}" />
                            </Listeners>
                        </ext:TextField>
                        <ext:Button ID="btnToolbarSearch" runat="server" Text="Search" Icon="Magnifier">
                            <Listeners>
                                <Click Handler="searchOrder(#{GridPanel1});" />
                            </Listeners>
                        </ext:Button>
                        <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                        <ext:Button ID="btnToolbarDelete" runat="server" Text="Delete Selected" Icon="Delete">
                            <Listeners>
                                <Click Handler="deleteOrders();" />
                            </Listeners>
                        </ext:Button>
                    </Items>
                </ext:Toolbar>
            </TopBar>
            <BottomBar>
                <ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="100" DisplayInfo="true"
                    DisplayMsg="Displaying orders {0} - {1} of {2}" EmptyMsg="No orders to display"
                    AutoWidth="true" />
            </BottomBar>
            <LoadMask ShowMask="true" />
            <SelectionModel>
                <ext:CheckboxSelectionModel ID="CheckboxSelectionModel1" runat="server" ColumnPosition="8"
                    AllowDeselect="true" CheckOnly="true" />
            </SelectionModel>
            <Plugins>
                <ext:RowEditor ID="rowedi" runat="server" SaveText="Update" ClicksToEdit="2" ErrorSummary="false"
                    ShowTooltip="={Ext.emptyFn}">
                    <Listeners>
                        <AfterEdit Fn="afterEdit" />
                    </Listeners>
                </ext:RowEditor>
            </Plugins>
            <Listeners>
                <Command Handler="CommandHandler(command, rowIndex, this)" />
            </Listeners>
            <View>
                <ext:GridView ID="GridView1" runat="server" ForceFit="true" />
            </View>
        </ext:GridPanel>
    </div>
    <script type="text/javascript">
        Ext.override(Ext.grid.GridView, {
            getEditorParent: function () {
                return document.body;
            }
        });
    </script>
</asp:Content>
